﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace 提交管理平台V1
{
    public partial class frmLogin : Form
    {
        SqlTools sqt;

        string strSelectUsers="select Uname 用户名称 from userlist where Mname='TJGL' and Unuser =0 order by Uname";

        public frmLogin()
        {
            InitializeComponent();
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            sqt = new SqlTools(GeneralTools.GetConnectionString(提交管理平台V1.Properties.Settings.Default.Host, "DefualtDataBase"));
            iniComboBox();
            if (cmbUserName.Items.Count == 0)
            {
                frmUserManage newFrmUserManage = new frmUserManage();
                newFrmUserManage.ShowDialog();
                iniComboBox();
                if (cmbUserName.Items.Count == 0)
                {
                    Application.Exit();
                }
            }
            cmbUserName.Focus();
        }

        private void iniComboBox()
        {
            sqt.FillComboBox(cmbUserName, strSelectUsers);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (cmbUserName.Text.Equals(string.Empty))
            {
                GeneralTools.ShowInfoMsg("用户名称不能为空！");
                cmbUserName.Focus();
                return;
            }
            UserInfo ui = new UserInfo(cmbUserName.Text, txtPassword.Text);
            if (ui.LoginSuccess)
            {
                frmMain newFrmMain = new frmMain(ui);
                //newFrmMain.WindowState = FormWindowState.Maximized;
                newFrmMain.Show();
                this.Close();
            }
            else
            {
                GeneralTools.ShowErrorMsg("登录失败：" + ui.LoginErrMsg);
                return;
            }
        }

        private void txtPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                btnLogin_Click(this, new EventArgs());
            }
        }
    }
}